package com.module.admin.tts.controller;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.module.admin.BaseController;
import com.module.admin.tts.enums.ServInfoStatus;
import com.ms.client.task.api.pojo.ServInfo;
import com.ms.client.task.api.service.ApiServInfoService;
import com.ms.client.task.api.service.ApiTaskJobService;
import com.ms.msclient.MsClientResponse;
import com.system.comm.model.Page;
import com.system.comm.utils.FrameJsonUtil;
import com.system.comm.utils.FrameMapUtil;

/**
 * task_project的Controller
 * @author yuejing
 * @date 2015-03-30 14:07:28
 * @version V1.0.0
 */
@Controller
public class TtsServInfoController extends BaseController {

	@Autowired
	private ApiServInfoService apiServInfoService;
	@Autowired
	private ApiTaskJobService apiTaskJobService;
	
	/**
	 * 跳转到管理页
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/ttsServInfo/f-view/manager")
	public String manger(HttpServletRequest request, ModelMap modelMap) {
		return "admin/tts/serv/servInfo-manager";
	}

	@RequestMapping(value = "/ttsServInfo/f-view/chart")
	public String chart(HttpServletRequest request, ModelMap modelMap) {
		List<ServInfo> servInfos = apiServInfoService.findByStatus(ServInfoStatus.NORMAL.getCode()).getBody();
		List<Map<String, Object>> servCounts = apiTaskJobService.findServidCount().getBody();
		for (ServInfo si : servInfos) {
			//设置任务总数
			for (Map<String, Object> map : servCounts) {
				String servid = si.getServid();//FrameMapUtil.getString(si, "servid");
				if(servid.equals(FrameMapUtil.getString(map, "servid"))) {
					//si.put("jobnum", FrameMapUtil.getInteger(map, "total"));
					si.setJobnum(FrameMapUtil.getInteger(map, "total"));
					break;
				}
			}
		}
		modelMap.put("servInfosJson", FrameJsonUtil.toString(servInfos));
		return "admin/tts/serv/servInfo-chart";
	}
	
	@RequestMapping(value = "/ttsServInfo/f-json/pageQuery")
	@ResponseBody
	public void pageQuery(HttpServletRequest request, HttpServletResponse response, ServInfo servInfo) {
		MsClientResponse<Page<ServInfo>> frame = apiServInfoService.pageQuery(servInfo);
		writerJson(response, frame);
	}
	
}